import 'package:flutter/material.dart';
import 'package:flutter_demo/components/root_page_head.dart';
import 'package:flutter_demo/http/http.dart';
import 'package:flutter_demo/view/root_pages/sub_pages/article_page.dart';
import 'package:flutter_demo/view/root_pages/sub_pages/recommend_page.dart';
import 'package:flutter_demo/view/root_pages/sub_pages/singer_page.dart';
import 'package:flutter_demo/view/root_pages/sub_pages/song_page.dart';
import 'package:flutter_demo/view/root_pages/sub_pages/tiny_video_page.dart';
import 'package:flutter_demo/view/root_pages/sub_pages/video_page.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

const List<Tab> _tabs = [
  Tab(text: '歌曲'),
  Tab(text: '推荐'),
  Tab(text: '歌手'),
  Tab(text: '小视频'),
  Tab(text: '文章'),
  Tab(text: '视频'),
];

final List<Widget> _tabsContent = [
  SongPage(),
  RecommendPage(),
  SingerPage(),
  TinyVideoPage(),
  ArticlePage(),
  VideoPage(),
];

class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
  TabController _tabController;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    print('2222');

    _tabController = TabController(
      initialIndex: 1,
      length: _tabs.length,
      vsync: this,
    );

    getList();

    // 监听切换
    // _tabController.addListener(() {
    //   // _tabController.index;
    //   print(_tabController.index);
    // });
  }

  Future getList() async {
    final result = await Http.get('/api/song/info/2');
    print(result);
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: RootPageHead(),
        bottom: TabBar(
          tabs: _tabs,
          controller: _tabController,
          isScrollable: true,
        ),
      ),
      body: TabBarView(
        children: _tabsContent,
        controller: _tabController,
      ),
    );
  }
}
